package com.meiyu.live.ui.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.eventbus.LoginEvent;
import com.meiyu.live.eventbus.UpdateRecommendEvent;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.net.response.IndexResponse;
import com.meiyu.live.popj.Advert;
import com.meiyu.live.popj.Assort;
import com.meiyu.live.popj.Column;
import com.meiyu.live.ui.main.widget.ColumnItemView;
import com.meiyu.live.util.DisplayUtils;
import com.meiyu.live.widget.Banner;
import com.meiyu.live.widget.BannerConfig;

import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends BaseFragment {

    private String[] bannerImages, bannerTitles;

    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private RecommendListAdapter adapter;
    private Banner banner;

    private IndexResponse indexResponse;

    public RecommendFragment() {
    }

    public static RecommendFragment newInstance() {
        RecommendFragment fragment = new RecommendFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getContextView() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void init() {
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        listView = (ListView) findViewById(R.id.listview);
        FrameLayout frameLayout = new FrameLayout(getActivity());
        banner = new Banner(getActivity());
        banner.setTitleBackgroundColor(0x80000000);
        banner.setTitleTextColor(0xffffffff);
        banner.setTitleTextSize(14);
        banner.setTitleHeight(DisplayUtils.dpToPixel(getActivity(), 40));
        frameLayout.addView(banner, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dpToPixel(getActivity(), 200)));
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        listView.addHeaderView(frameLayout);
//        banner.setBannerTitle(bannerTitles);
//        banner.setImages(bannerImages);
        adapter = new RecommendListAdapter();
        listView.setAdapter(adapter);

        getData(true);
    }

    private void getData(final boolean reload) {
        //首页获取数据接口
        request(new BaseBodyParams(Constant.UrlParams.INDEX), IndexResponse.class, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse baseResponse) {
                refreshLayout.setRefreshing(false);
                IndexResponse indexResponse = (IndexResponse) baseResponse;
                if (indexResponse != null) {
                    RecommendFragment.this.indexResponse = indexResponse;
                    adapter.notifyDataSetChanged();
                    updateBannerData();
                    updateFollowedColumn();
                }
            }
        });
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        refreshLayout.setRefreshing(false);
        super.onErrorResponse(volleyError);
    }

    private void updateBannerData() {
        if (indexResponse != null && indexResponse.getAd() != null && indexResponse.getAd().size() > 0) {
            final int listSize = indexResponse.getAd().size();
            bannerImages = new String[listSize];
            bannerTitles = new String[listSize];
            for (int i = 0; i < listSize; i++) {
                final Advert advert = indexResponse.getAd().get(i);
                bannerImages[i] = advert.getImage_url();
                bannerTitles[i] = advert.getDescription();
            }
            banner.setBannerTitle(bannerTitles);
            banner.setImages(bannerImages);
        }
    }

    class RecommendListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return indexResponse != null && indexResponse.getCat() != null ? indexResponse.getCat().size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ColumnItemView columnItemView = null;
            if (convertView == null) {
                columnItemView = new ColumnItemView(getActivity());
                convertView = columnItemView;
            } else {
                columnItemView = (ColumnItemView) convertView;
            }
            columnItemView.setData(indexResponse.getCat().get(position));
            return convertView;
        }
    }

    public void onEvent(UpdateRecommendEvent event){
        getData(true);
    }

    public void onEvent(LoginEvent event){
        getData(true);
    }

    private static HashMap<Long,Assort> followedColumnList = new HashMap<>();
    private void updateFollowedColumn(){
        if (indexResponse != null && indexResponse.getCat() != null) {
            followedColumnList.clear();
            for(Column bean : indexResponse.getCat()){
                Assort assort = new Assort(bean.getAssort_id(),bean.getAssort_name());
                followedColumnList.put(assort.getId(),assort);
            }
        }
    }
    public static HashMap<Long,Assort> getFollowedColumn(){
        return followedColumnList;
    }
}
